package com.yanqu.road.server.gameplayer.module.activity.qqprivilege;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ppl.PplUserData;
import com.yanqu.road.entity.activity.qqprivilege.QQPrivilegeUserData;
import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.task.args.activity.TongBingRedPacketValueArgs;
import com.yanqu.road.logic.bussiness.activity.PplBussiness;
import com.yanqu.road.logic.bussiness.activity.QQPrivilegeBussiness;
import com.yanqu.road.pb.activity.qqprivilege.QQPrivilege;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.server.manger.activity.qqprivilege.QQPrivilegeMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.Random;

public class QQPrivilegeActivityModule extends GeneralModule {

    private QQPrivilegeUserData userData;
    public QQPrivilegeActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        ActivityInfo activityInfo = QQPrivilegeMgr.getActivityInfo();
        if (activityInfo == null) {
            return true;
        }
        if (isAble()) {
            userData = QQPrivilegeBussiness.getQQPrivilegeUserData(player.getUserId(), activityInfo.getActivityId());
            if (userData == null) {
                initUserData();
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        activityLogin();
        return true;
    }

    @Override
    public boolean saveData() {
        if (userData != null) {
            if (userData.isInsertOption()) {
                QQPrivilegeBussiness.addQQPrivilegeUserData(userData);
            } else if (userData.isUpdateOption()) {
                QQPrivilegeBussiness.updateQQPrivilegeUserData(userData);
            }
        }
        return true;
    }

    /**
     * 刷新活动 || 登录
     */
    public void activityLogin() {
        if (isAble()) {
            initUserData();
            syncUserData();
        }
    }

    public synchronized void initUserData() {
        ActivityInfo activityInfo = QQPrivilegeMgr.getActivityInfo();
        if (activityInfo == null) {
            return;
        }
        if (userData == null && isAble()) {
            QQPrivilegeUserData tmpUserData = new QQPrivilegeUserData();
            tmpUserData.setActivityId(activityInfo.getActivityId());
            tmpUserData.setUserId(player.getUserId());
            tmpUserData.setDayLoginRewardTime(0);
            tmpUserData.setGotRegisterReward(QQPrivilegeMgr.isGotRegisterReward(tmpUserData, player) ? 1 : 0);
            tmpUserData.setGotProgressReward(1);
            tmpUserData.setInsertOption();
            userData = tmpUserData;
            player.notifyListenerAndSync(eGamePlayerEventType.QQPrivilegeLv.getValue(), player.getUserInfo().getTitleId());
        }
    }

    /*
     * 身份等级变化
     */
    public void onTitleChange() {
        if (isAble()) {
            player.notifyListenerAndSync(eGamePlayerEventType.QQPrivilegeLv.getValue(), player.getUserInfo().getTitleId());
        }
    }

    /*
     * 是否有参与资格
     */
    public boolean isAble() {
        ActivityInfo activityInfo = QQPrivilegeMgr.getActivityInfo();
        if (activityInfo == null) {
            return false;
        }
        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        return inShowTime()
                && SystemOpenMgr.systemOpen(player, eSystemId.QQPrivilege.getValue())
                && QQPrivilegeMgr.isAble(player.getAppId(), channelId);
    }

    /*
     * 同步用户数据
     */
    public void syncUserData() {
        if (userData != null && inShowTime() && isAble()) {
            QQPrivilege.QQPrivilegeUserDataSyncMsg.Builder resp = QQPrivilege.QQPrivilegeUserDataSyncMsg.newBuilder();
            resp.setActivityId(userData.getActivityId());
            resp.setDayLoginRewardTime(userData.getDayLoginRewardTime());
            resp.setGotRegisterReward(QQPrivilegeMgr.isGotRegisterReward(userData, player));
            player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_USER_DATA_SYNC, resp);
        }
    }

    /*
     * inShowTime
     */
    private boolean inShowTime() {
        if (!ActivityMgr.activityInShowTime(QQPrivilegeMgr.getActivityInfo())) {
            return false;
        }
        return true;
    }

    /*
     * 领取每日登陆奖励
     */
    public void getDailyReward() {
        QQPrivilege.QQPrivilegeGetDailyRewardResp.Builder builder = QQPrivilege.QQPrivilegeGetDailyRewardResp.newBuilder();
        if (isAble()) {
            builder.setRet(GameErrorCode.E_QQ_PRIVILEGE_NOT_ABLE);
            player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_GET_DAILY_REWARD, builder);
            return;
        }
        if (userData.getDayLoginRewardTime() > DateHelper.getTodayZeroTimeStamp()) {
            builder.setRet(GameErrorCode.E_QQ_PRIVILEGE_GOT);
            player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_GET_DAILY_REWARD, builder);
            return;
        }
        // 先设置时间戳
        userData.setDayLoginRewardTime(System.currentTimeMillis());

        // 给奖励
        player.getModule(CurrencyModule.class).addCurrency(QQPrivilegeMgr.getDailySignReward(), eLogMoneyType.QQPrivilege, eLogMoneyType.QQPrivilegeAddReward);

        // 回消息
        builder.setRet(0);
        builder.setReward(QQPrivilegeMgr.getDailySignReward());
        player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_GET_DAILY_REWARD, builder);
    }

    /*
     * 领取注册奖励
     */
    public void getRegisterReward() {
        QQPrivilege.QQPrivilegeGetRegisterRewardResp.Builder builder = QQPrivilege.QQPrivilegeGetRegisterRewardResp.newBuilder();
        if (isAble()) {
            builder.setRet(GameErrorCode.E_QQ_PRIVILEGE_NOT_ABLE);
            player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_GET_REGISTER_REWARD, builder);
            return;
        }
        if (userData.getGotRegisterReward() == 1) {
            builder.setRet(GameErrorCode.E_QQ_PRIVILEGE_GOT);
            player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_GET_REGISTER_REWARD, builder);
            return;
        }
        // 先设置
        userData.setGotRegisterReward(1);

        // 给奖励
        player.getModule(CurrencyModule.class).addCurrency(QQPrivilegeMgr.getAccountEnrollReward(), eLogMoneyType.QQPrivilege, eLogMoneyType.QQPrivilegeAddReward);

        // 回消息
        builder.setRet(0);
        builder.setReward(QQPrivilegeMgr.getAccountEnrollReward());
        player.sendPacket(ClientProtocol.U_QQ_PRIVILEGE_GET_REGISTER_REWARD, builder);
    }
}
